package com.tree.backend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tree.backend.entity.pojo.PartyProductionLink;
import com.tree.backend.entity.vo.PartyProductionLinkData;
import com.tree.backend.entity.vo.ProductionTrendData;
import com.tree.backend.entity.vo.QualityAnalysisData;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 党建与生产关联表，记录党建活动对生产经营的具体贡献和影响 Mapper 接口
 * </p>
 *
 * @author tree
 * @since 2025-10-15
 */
public interface PartyProductionLinkMapper extends BaseMapper<PartyProductionLink> {

    @Select("select po.org_name as \"organizations\",\n" +
            "       round(avg(pe.quality_rate)*100,1) as \"qualityRates\",\n" +
            "       round(avg(ppl.quality_improvement)*100,1) as \"qualityImprovements\"\n" +
            "from party_organization po\n" +
            "left join production_efficiency pe on po.org_code = pe.org_code\n" +
            "left join party_production_link ppl on po.org_code = ppl.org_code\n" +
            "group by po.org_name;")
    List<QualityAnalysisData> qualityAnalysis();

    @Select("select record_month as\"month\",sum(production_value) as\"productionValue\"\n" +
            "from production_efficiency\n" +
            "group by month;")
    List<ProductionTrendData> efficiencyTrend();

    @Select("select org_name as\"organizationName\",\n" +
            "       round(avg(ppl.production_impact)*100,2) AS \"productionImpact\",\n" +
            "       sum(ppl.innovation_contribution) As \"innovationContribution\",\n" +
            "       round(avg(ppl.quality_improvement)*100,2) as \"qualityImprovement\",\n" +
            "       sum(cost_saving) as \"costSaving\"\n" +
            "       from\n" +
            "party_organization po join party_production_link ppl on po.org_code = ppl.org_code\n" +
            "group by org_name;")
    List<PartyProductionLinkData> link();
}
